<?php

namespace app\api\controller;

use addons\jiezicommon\service\Tools;
use app\common\controller\Api;

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['get_list', 'get_detail', 'get_cat'];
    protected $noNeedRight = '*';

    /**
     * 商品列表
     * @ApiParams (name="page", type="int", required=false, sample="1", description="页码,默认=1", default="1")
     * @ApiParams (name="limit", type="int", required=false, sample="10", description="每页条数,默认=10", default="10")
     * @ApiParams (name="cat_id", type="int", required=false, sample="1", description="商品类型:1=普通商品,2=特殊商品", default="1")
     */
    public function get_list(){
        $params = input();
        $page = empty($params['page']) ? 1 : intval($params['page']);
        $limit = empty($params['limit']) ? 10 : intval($params['limit']);
        $cat_id = empty($params['cat_id']) ? 0 : intval($params['cat_id']);
        if($cat_id < 0){
            $cat_id = 0;
        }
        $return_data['list'] = [];
        $table = \app\common\model\Goods::alias('t');
        $fields = 'id,name,mktprice,price,images,unit';
        if($cat_id){
            $table->where('goods_cat_id', $cat_id);
        }
        $table->where('marketable', '1');
        $res = $table->field($fields)->order('weigh')
            ->paginate($limit, false, ['page' => $page])
            ->each(function($item){
                $item['images'] = $item['images'] ? Tools::str2arr($item['images'], ',', 'cdnurl', true, [true]) : [];
                $item['image'] = !empty($item['images']) ? $item['images'][0] : '';
                $item['price'] = floatval($item['price']);
                $item['mktprice'] = floatval($item['mktprice']);
            });
        $sql = $table->getLastSql();
        $list = $res->items();
        $return_data['list'] = $list;
        $return_data['total'] = $res->total();
        $return_data['sql'] = $sql;
        $this->success('', $return_data);
    }

    /**
     * 商品详细
     * @ApiParams (name="id", type="int", required=true, sample="1", description="商品ID", default="1")
     */
    public function get_detail($id = null){
        $return_data = [
            'id' => $id,
            'info' => [],
        ];
        $table = \app\common\model\Goods::alias('t');
        $fields = 'id,name,mktprice,price,images,unit,spes_desc,intro';
        $table->where('marketable', '1');
        $res = $table->field($fields)->find($id);
        if($res){
            $res['images'] = $res['images'] ? Tools::str2arr($res['images'], ',', 'cdnurl', true, [true]) : [];
            $res['image'] = !empty($res['images']) ? $res['images'][0] : '';
            $res['price'] = floatval($res['price']);
            $res['mktprice'] = floatval($res['mktprice']);
            $spes_desc = $res['spes_desc'];
            unset($res['spes_desc']);
//            echo var_export(json_decode($res['spes_desc']), 1);die;
            $res->spes = $spes_desc ? json_decode($spes_desc, 1) : [];
            $return_data['info'] = $res;
            $return_data['info']['admin_tel'] = array_filter(explode("\r\n",config('site.admin_tel')));
            $return_data['admin_tel'] = array_filter(explode("\r\n",config('site.admin_tel')));
            $this->success('', $return_data);
        }else{
            $this->error('商品不存在');
        }
    }

    /**
     * 商品分类
     */
    public function get_cat(){
        $return_data = [
            'list' => [],
        ];
        $table = \app\common\model\GoodsCat::alias('t');
        $fields = 'id,name';
        $table->where('status', '1');
        $res = $table->field($fields)->order('weigh')->select();
        $return_data['list'] = $res;
        $this->success('', $return_data);
    }
}
